package com.deyuanyun.pic.ae.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.deyuanyun.pic.ae.controller.dto.AEPipeLineQuery;
import com.deyuanyun.pic.ae.domain.AEPipeline;
import com.deyuanyun.pic.ae.domain.AEPipelineExample;
import com.deyuanyun.pic.ae.enumeration.DataStatedEnum;
import com.deyuanyun.pic.ae.enumeration.IsNewDateEnum;
import com.deyuanyun.pic.ae.enumeration.PositionRelationEnum;
import com.deyuanyun.pic.ae.mapper.AEPipelineMapper;
import com.deyuanyun.pic.ae.mapper.dao.AEPipelineDao;
import com.deyuanyun.pic.ae.service.AEBaseService;
import com.deyuanyun.pic.ae.service.AEPipeLineService;
import com.deyuanyun.pic.common.util.CommonUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.common.webservice.UtilsServiceClient;
import com.deyuanyun.pic.pipe.domain.Identification;
import com.deyuanyun.pic.pipe.mapper.dao.PipeGeneralDao;
import com.deyuanyun.pic.pipe.util.PipeGeneralUtil;
import com.deyuanyun.pic.settings.support.biz.AttachmentUtil;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * “第三方管道及设施” 服务接口 实现类
 *
 * @author huanglw
 * @date  2016-11-9
 *
 * History: [修改人][时间][修改描述]
 */
@Service
public class AEPipeLineServiceImpl extends AEBaseService implements AEPipeLineService {

    @Resource
    private AEPipelineMapper aePipelineMapper;
    @Resource
    private AEPipelineDao aePipelineDao;

    @Override
    public Page<AEPipeline> query(AEPipeLineQuery aePipeLineQuery) {
        return aePipelineDao.query(aePipeLineQuery);
    }

    @Override
    public AEPipeline find(String id) {
        return aePipelineMapper.selectByPrimaryKey(id);
    }

    @Override
    public int edit(AEPipeline aePipeline, List<String> imgs) {

        if (PositionRelationEnum.CROSS.toString().equals(aePipeline.getRelativePositionRelation())) {
            // 计算交叉绝对里程
            PositionObj crossPositionObj = getPositionInfo(aePipeline.getPipeLineId(), aePipeline.getCrossX(), aePipeline.getCrossY());
            if (crossPositionObj != null) {
                aePipeline.setCrossMileage(crossPositionObj.getMileage());
                aePipeline.setCrossNagm(crossPositionObj.getNagm());
                aePipeline.setCrossNagmNumber(crossPositionObj.getNagmNumber());
                aePipeline.setCrossNagmMileage(crossPositionObj.getNagmMileage());
            }
        } else {
            // 计算起始绝对里程
            PositionObj beginPositionObj = getPositionInfo(aePipeline.getPipeLineId(), aePipeline.getBeginX(), aePipeline.getBeginY());
            if (beginPositionObj != null) {
                aePipeline.setBeginMileage(beginPositionObj.getMileage());
                aePipeline.setBeginNagm(beginPositionObj.getNagm());
                aePipeline.setBeginNagmNumber(beginPositionObj.getNagmNumber());
                aePipeline.setBeginNagmMileage(beginPositionObj.getNagmMileage());
            }
            // 计算终点绝对里程
            PositionObj endPositionObj = getPositionInfo(aePipeline.getPipeLineId(), aePipeline.getEndX(), aePipeline.getEndY());
            if (endPositionObj != null) {
                aePipeline.setEndMileage(endPositionObj.getMileage());
                aePipeline.setEndNagm(endPositionObj.getNagm());
                aePipeline.setEndNagmNumber(endPositionObj.getNagmNumber());
                aePipeline.setEndNagmMileage(endPositionObj.getNagmMileage());
            }
        }

        // 添加 图片信息
        String bizzId = StringUtils.isNotNullEmpty(aePipeline.getId()) ? aePipeline.getId() : CommonUtil.getUUID();
        for (String id : imgs) {
            AttachmentUtil.updateBusinessId(bizzId, id);
        }

        if (StringUtils.isNullEmpty(aePipeline.getId())){
            AEPipelineExample aePipelineExample = new AEPipelineExample();
            aePipelineExample.createCriteria().andDeletedEqualTo(Short.valueOf("0")).andNameEqualTo(aePipeline.getName());
            int countNum = aePipelineMapper.countByExample(aePipelineExample);
            Asserts.assertLogic(countNum > 0, " :“"+aePipeline.getName()+"”第三方管道及设施 已经存在，不能重复插入");

            aePipeline.setId(bizzId);
            aePipeline.setCreateDate(new Date());
            aePipeline.setCreateUser(realName);
            aePipeline.setDataStated(DataStatedEnum.ALREADY_RELEASED.getCode());
            aePipeline.setIsNewest(Short.valueOf(IsNewDateEnum.YES.getCode()));
            return aePipelineMapper.insertSelective(aePipeline);
        }

        AEPipeline aePipelineObj = this.find(aePipeline.getId());
        Asserts.assertLogic(aePipelineObj == null, "第三方管道及设施 不存在");
        Asserts.assertLogic(isAuditReleased(aePipelineObj.getDataStated()), "该 第三方管道及设施 已审核通过,不允许编辑");
        if (isAuditDismissed(aePipelineObj.getDataStated())){
            aePipeline.setDataStated(DataStatedEnum.ALREADY_RELEASED.getCode());
        }
        return aePipelineMapper.updateByPrimaryKeySelective(aePipeline);
    }

    @Override
    public int audit(AEPipeline aePipeline) {
        AEPipeline aePipelineObj = this.find(aePipeline.getId());
        Asserts.assertLogic(aePipelineObj == null, "第三方管道及设施 不存在");
        Asserts.assertLogic(isAudit(aePipelineObj.getDataStated()), "该 第三方管道及设施 已审核,不能重复审核");
        return aePipelineMapper.updateByPrimaryKeySelective(aePipeline);
    }

    @Override
    public int delete(String id) {
        AEPipeline aePipeline = this.find(id);
        Asserts.assertLogic(aePipeline == null, "第三方管道及设施 不存在");
        Asserts.assertLogic(isAuditReleased(aePipeline.getDataStated()), "该 第三方管道及设施 已审核通过,不能“删除”");
        aePipeline = new AEPipeline();
        aePipeline.setId(id);
        aePipeline.setDeleted(Short.valueOf("1"));
        aePipeline.setUpdateDate(new Date());
        aePipeline.setUpdateUser(realName);
        aePipeline.setUpdateRemark("删除");
        return aePipelineMapper.updateByPrimaryKeySelective(aePipeline);
    }

}
